package com.fis.vaadin.util;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class ClientAddressUtil {
	public static String getClientIP() {

		try {

			// MainApplication mainApp = AppClient.getApplication();
			// return mainApp.getIpRemoteAddr() == null ? "" : mainApp
			// .getIpRemoteAddr();

			String strJavaScript = "function myIP(){ \n"
					+ " var yip2 = java.net.InetAddress.getLocalHost(); \n"
					+ " var yip = yip2.getHostAddress(); \n"
					+ " return yip; \n" + " }\n";

			ScriptEngineManager manager = new ScriptEngineManager();
			ScriptEngine engine = manager.getEngineByName("javascript");

			engine.eval(strJavaScript);

			String name = ((Invocable) engine).invokeFunction("myIP")
					.toString();

			return name;

		} catch (Exception e) {

			e.printStackTrace();
			return null;

		}

	}
}
